#!/usr/bin/env python3
"""
SERP模块使用示例
演示如何生成Google搜索查询语句
"""

import sys
import os

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from crawler.serp import (
    SERPQueryGenerator, 
    create_google_search_url,
    generate_site_query,
    generate_all_queries,
    get_supported_categories,
    INDUSTRY_CATEGORIES
)

def demo_basic_usage():
    """演示基本用法"""
    print("🔍 SERP查询生成器基本用法演示")
    print("=" * 50)
    
    # 创建生成器实例
    generator = SERPQueryGenerator()
    
    # 示例URL
    test_url = "https://metapress.com"
    
    print(f"目标网站: {test_url}")
    print()
    
    # 1. 为单个类别生成查询
    print("1. 单个类别查询生成:")
    category = "Home & Living"
    query = generator.generate_category_query(test_url, category)
    print(f"   类别: {category}")
    print(f"   查询: {query}")
    
    # 生成Google搜索URL
    search_url = create_google_search_url(query)
    print(f"   搜索URL: {search_url[:80]}...")
    print()
    
    # 2. 生成关键词查询
    print("2. 关键词查询生成:")
    keyword_query = generator.generate_keyword_query(test_url, category)
    print(f"   类别: {category}")
    print(f"   关键词查询: {keyword_query}")
    print()

def demo_all_categories():
    """演示所有类别的查询生成"""
    print("📋 所有行业类别查询生成")
    print("=" * 50)
    
    test_url = "techcrunch.com"
    
    # 生成所有类别的查询
    all_queries = generate_all_queries(test_url)
    
    print(f"为 {test_url} 生成的所有查询:")
    print()
    
    for i, (category, query) in enumerate(all_queries.items(), 1):
        print(f"{i:2d}. {category}:")
        print(f"    {query}")
        print()

def demo_category_search():
    """演示类别搜索功能"""
    print("🔎 行业类别搜索演示")
    print("=" * 50)
    
    generator = SERPQueryGenerator()
    
    # 搜索关键词
    search_keywords = ["technology", "health", "business", "travel"]
    
    for keyword in search_keywords:
        matching_categories = generator.search_categories_by_keyword(keyword)
        print(f"关键词 '{keyword}' 匹配的类别:")
        for category in matching_categories:
            print(f"  - {category}")
        print()

def demo_category_details():
    """演示类别详细信息"""
    print("📊 行业类别详细信息")
    print("=" * 50)
    
    generator = SERPQueryGenerator()
    
    # 展示几个类别的详细信息
    sample_categories = ["Technology & Computing", "Health & Lifestyle", "Business & Finance"]
    
    for category in sample_categories:
        info = generator.get_category_info(category)
        print(f"类别: {category}")
        print(f"子分类 ({len(info['subcategories'])}个):")
        for subcategory in info['subcategories']:
            print(f"  - {subcategory}")
        
        print(f"关键词 ({len(info['keywords'])}个):")
        for keyword in info['keywords'][:5]:  # 只显示前5个关键词
            print(f"  - {keyword}")
        if len(info['keywords']) > 5:
            print(f"  ... 还有 {len(info['keywords']) - 5} 个关键词")
        print()

def demo_practical_example():
    """实际应用示例"""
    print("🚀 实际应用示例")
    print("=" * 50)
    
    # 模拟实际使用场景
    websites = [
        "https://metapress.com",
        "techcrunch.com",
        "www.amazon.com",
        "https://www.nike.com"
    ]
    
    target_categories = ["Technology & Computing", "Home & Living", "Business & Finance"]
    
    generator = SERPQueryGenerator()
    
    print("为多个网站生成特定类别的查询:")
    print()
    
    for website in websites:
        print(f"网站: {website}")
        
        for category in target_categories:
            try:
                query = generator.generate_category_query(website, category)
                print(f"  {category}:")
                print(f"    {query}")
            except Exception as e:
                print(f"  {category}: 错误 - {e}")
        print()

def demo_url_cleaning():
    """演示URL清理功能"""
    print("🧹 URL清理功能演示")
    print("=" * 50)
    
    generator = SERPQueryGenerator()
    
    # 测试各种URL格式
    test_urls = [
        "metapress.com",                          # 无协议
        "http://example.com",                     # HTTP协议
        "https://www.google.com/search?q=test",   # 带查询参数
        "https://github.com/user/repo#readme",    # 带fragment
        "ftp://files.example.com/file.txt",       # 其他协议
        "www.site.com/path/to/page",             # 带路径
    ]
    
    print("URL清理测试:")
    print()
    
    for url in test_urls:
        try:
            clean_url = generator.clean_url(url)
            print(f"原始: {url}")
            print(f"清理: {clean_url}")
            print()
        except Exception as e:
            print(f"原始: {url}")
            print(f"错误: {e}")
            print()

def show_all_categories():
    """显示所有支持的行业类别"""
    print("📚 所有支持的行业类别")
    print("=" * 50)
    
    categories = get_supported_categories()
    
    for i, category in enumerate(categories, 1):
        subcategories = INDUSTRY_CATEGORIES[category]
        print(f"{i:2d}. {category} ({len(subcategories)}个子分类)")
        print(f"    子分类: {', '.join(subcategories[:3])}{'...' if len(subcategories) > 3 else ''}")
        print()

def main():
    """主函数"""
    print("SERP查询生成器演示程序")
    print("🌐 用于生成Google搜索查询语句的工具")
    print()
    
    # 运行所有演示
    demo_basic_usage()
    print("\n" + "="*80 + "\n")
    
    show_all_categories()
    print("\n" + "="*80 + "\n")
    
    demo_category_details()
    print("\n" + "="*80 + "\n")
    
    demo_url_cleaning()
    print("\n" + "="*80 + "\n")
    
    demo_category_search()
    print("\n" + "="*80 + "\n")
    
    demo_practical_example()
    print("\n" + "="*80 + "\n")
    
    demo_all_categories()
    
    print("\n" + "🎉 演示完成！")
    print("您可以在自己的项目中使用这些功能来生成Google搜索查询。")

if __name__ == "__main__":
    main() 